package com.zygh.lz.service.role;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zygh.lz.dao.AccountMapper;
import com.zygh.lz.dao.StaffMapper;
import com.zygh.lz.dao.role.SysUserRoleMapper;
import com.zygh.lz.entity.role.SysMenu;
import com.zygh.lz.entity.role.SysRole;
import com.zygh.lz.entity.role.SysUserRole;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> {

    @Resource
    private SysRoleServiceImpl sysRoleService;

    @Resource
    private StaffMapper staffMapper;
    @Resource
    private SysMenuServiceImpl sysMenuService;
    @Resource
    private AccountMapper accountMapper;

    public String getUserAuthorityInfo(Long userId) {

        String authority = "";

        // 获取角色编码
        List<SysRole> roles = sysRoleService.list(new QueryWrapper<SysRole>()
                .inSql("id", "select role_id from sys_user_role where user_id = " + userId.intValue()));

        if (roles.size() > 0) {
            String roleCodes = roles.stream().map(r -> "ROLE_" + r.getCode()).collect(Collectors.joining(","));
            authority = roleCodes.concat(",");
        }

        // 获取菜单操作编码
        //List<Long> menuIds = staffMapper.getNavMenuIds(userId);
        List<Long> menuIds = accountMapper.getNavMenuIds(userId);
        if (menuIds.size() > 0) {

            List<SysMenu> menus = sysMenuService.listByIds(menuIds);
            String menuPerms = menus.stream().map(m -> m.getPerms()).collect(Collectors.joining(","));

            authority = authority.concat(menuPerms);
        }


        return authority;
    }


}
